package com.gdth.service;

import java.io.File;
import java.io.IOException;

import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;

import android.media.MediaRecorder;

/**
 * @author arvin
 *
 */
public class PhoneMediaRecorder {
 
    private MediaRecorder mMediaRecorder;
    
    /** 是否启动 **/
    private boolean started = false;
    
    /** 标志来电或拨号**/
    private boolean isCommingNumber = false;
    
    private String filePath;
    
    private String jtuuid;   
	private String number;
	private String jcuuid;
	private String uuid;
    
    private String type;
    
    private String typeName = ".3gp";
    
    public PhoneMediaRecorder() {
    	mMediaRecorder = new MediaRecorder();
    }
        
    public PhoneMediaRecorder(String number) {
    	mMediaRecorder = new MediaRecorder();
    	
        this.setNumber(number);
    }    
    
    public void start() {    
        started = true;
        
//        String callDir = "呼入";
//        if (isCommingNumber) callDir = "呼出";
        uuid = UUIDUtil.getUUID();
        File recordName = ToolUtil.createFile(uuid + typeName);
        filePath = recordName.getAbsolutePath();
        
        if (type == null)
        	type = "MIC";
        
        if (type.equals("MIC"))
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        else if (type.equals("CAMCORDER"))
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        else if (type.equals("VOICE_CALL"))
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        else if (type.equals("VOICE_COMMUNICATION"))
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
        else if (type.equals("VOICE_DOWNLINK"))
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
        else if (type.equals("VOICE_UPLINK"))
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
        else
        	mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
//        mMediaRecorder.setMaxDuration(MAX_LENGTH);
//        int max = MediaRecorder.getAudioSourceMax();
//        mediaRecorder.set
//        mediaRecorder.setAudioSamplingRate(100); 
            
        mMediaRecorder.setOutputFile(recordName.getAbsolutePath());
            
        try {    
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch (IllegalStateException e) {    
            e.printStackTrace();
        } catch (IOException e) {    
            e.printStackTrace();
        }    
      
        started = true;
    }    
    
    public void destory() {    
        try {    
            if (mMediaRecorder!=null) {    
                mMediaRecorder.stop();
                mMediaRecorder.release();
                mMediaRecorder = null;
            }    
            started = false;
        } catch (IllegalStateException e) {    
            e.printStackTrace();
        }
    } 
    
    public void stop() {    
        try {    
            if (mMediaRecorder!=null) {    
                mMediaRecorder.stop();
            }    
            started = false;
        } catch (IllegalStateException e) {    
            e.printStackTrace();
        }
    }    
        
    public void pause() {    
    }    
    
    
    /********************************getter,setter*********************************/
    public boolean isStarted() {    
        return started;
    }    
    
    public void setStarted(boolean hasStarted) {    
        this.started = hasStarted;
    }    
    
    public boolean isCommingNumber() {    
    	return isCommingNumber;
    }    

    public void setIsCommingNumber(boolean isCommingNumber) {    
    	this.isCommingNumber = isCommingNumber;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getJtuuid() {
		return jtuuid;
	}

	public void setJtuuid(String jtuuid) {
		this.jtuuid = jtuuid;
	}

	public String getJcuuid() {
		return jcuuid;
	}

	public void setJcuuid(String jcuuid) {
		this.jcuuid = jcuuid;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	
}



